package com.irate.activity;

import java.util.List;

import org.apache.http.Header;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.PopupWindow.OnDismissListener;

import com.irate.adapter.RatingCriteriaAdapter;
import com.irate.dao.RatingCriteriaDAO;
import com.irate.http.AsyncHttpResponseHandler;
import com.irate.model.RatingCriteria;
import com.irate.provider.ContentManager;

public class ListCriteriaActivity extends Activity {

	protected RatingCriteriaDAO criteriaDao;
	protected ProgressDialog dialog;
	protected ListView list_content_criteria;
	protected View mainView;
	protected View contentView;
	protected Button addNewCriteria;
	protected RelativeLayout rl;
	protected String cate_id;
	protected PopupWindow pw;
	protected Button criteriaOK;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		LayoutInflater inflat = LayoutInflater.from(this);
		mainView = inflat.inflate(R.layout.popup_list_criteria, null);

		setContentView(mainView);

		init();
		execute();

	}

	private void init() {
		criteriaDao = new RatingCriteriaDAO();
		list_content_criteria = (ListView) findViewById(R.id.list_content_criteria);

		LayoutInflater inflat = LayoutInflater.from(this);
		contentView = inflat.inflate(R.layout.popup_add_new_criteria, null);

		addNewCriteria = (Button) findViewById(R.id.addNewCriteria);
		rl = (RelativeLayout) findViewById(R.id.content_criteria_popup);
		criteriaOK = (Button) findViewById(R.id.criteriaOK);

	}

	private void execute() {
		int index = ContentManager.getInstance().getCategory_index_selected();
		cate_id = ContentManager.getInstance().getListRatingCategory()
				.get(index).getCategory_id();

		criteriaOK.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				
				
				onBackPressed();
			}
		});

		if (ContentManager.getInstance().getListRatingCriteria().size() == 0)
			getData();
		else {
			RatingCriteriaAdapter adapter = new RatingCriteriaAdapter(
					ListCriteriaActivity.this);
			list_content_criteria.setAdapter(adapter);
		}
		addNewCriteria.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				pw = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT,
						LayoutParams.WRAP_CONTENT, true);

				Button add_new_criteria_and_continute = (Button) contentView
						.findViewById(R.id.add_new_criteria_and_continute);

				final EditText new_criteria = (EditText) contentView
						.findViewById(R.id.new_criteria);

				add_new_criteria_and_continute
						.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								String str = new_criteria.getText().toString()
										.trim();
								if (!str.equals("")) {

									criteriaDao.addCriteria(addCriteria,
											ListCriteriaActivity.this, cate_id,
											str, null);
									pw.dismiss();
								} else {
									Toast.makeText(
											ListCriteriaActivity.this,
											getString(R.string.miss_information),
											Toast.LENGTH_SHORT).show();
								}

							}
						});

				pw.setBackgroundDrawable(new BitmapDrawable());
				pw.setAnimationStyle(android.R.style.Animation_Dialog);
				pw.showAtLocation(mainView, Gravity.CENTER, 0, 15);
				pw.setOnDismissListener(new OnDismissListener() {

					@Override
					public void onDismiss() {
						rl.setVisibility(View.VISIBLE);
					}
				});

				rl.setVisibility(View.INVISIBLE);
			}
		});

	}

	private void getData() {
		dialog = ProgressDialog.show(ListCriteriaActivity.this, getResources()
				.getString(R.string.get_criteria),
				getResources().getString(R.string.loading), true);

		criteriaDao.getListCriteria(handlerGetCriteria,
				ListCriteriaActivity.this, cate_id, dialog);
	}

	public void dimissWindow(View v) {
		onBackPressed();
	}

	public void clickNothing(View v) {
		// do nothing
	}

	AsyncHttpResponseHandler handlerGetCriteria = new AsyncHttpResponseHandler() {
		@Override
		public void onSuccess(int statusCode, Header[] headers, byte[] response) {

			System.out.println("Success register");

			List<RatingCriteria> ldata = criteriaDao.parseListData(new String(
					response));
			ContentManager.getInstance().setListRatingCriteria(ldata);

			RatingCriteriaAdapter adapter = new RatingCriteriaAdapter(
					ListCriteriaActivity.this);
			list_content_criteria.setAdapter(adapter);

		}

		@Override
		public void onStart() {
			System.out.println("Start");
		}

		@Override
		public void onFinish() {
			if (dialog != null)
				dialog.dismiss();
		}

		@Override
		public void onFailure(int statusCode, Header[] headers,
				byte[] errorResponse, Throwable e) {
			System.out.println("Fail Register");
			try {
				JSONObject obj = new JSONObject(new String(errorResponse));

				Toast.makeText(ListCriteriaActivity.this,
						obj.getString("message").toString(), Toast.LENGTH_SHORT)
						.show();

			} catch (Exception ex) {
				Toast.makeText(ListCriteriaActivity.this,
						getString(R.string.connection_error),
						Toast.LENGTH_SHORT).show();
			}

			onBackPressed();
		}
	};

	AsyncHttpResponseHandler addCriteria = new AsyncHttpResponseHandler() {
		@Override
		public void onSuccess(int statusCode, Header[] headers, byte[] response) {

			System.out.println("Success register");

			try {
				JSONObject obj = new JSONObject(new String(response));

				JSONObject criteriaObj = obj.getJSONObject("message");
				RatingCriteria rc = criteriaDao.parseData(criteriaObj);

				ContentManager.getInstance().getListRatingCriteria().add(rc);
				RatingCriteriaAdapter adapter = new RatingCriteriaAdapter(
						ListCriteriaActivity.this);
				list_content_criteria.setAdapter(adapter);

			} catch (Exception ex) {
				Toast.makeText(ListCriteriaActivity.this,
						getString(R.string.connection_error),
						Toast.LENGTH_SHORT).show();
			}

			// getData();

		}

		@Override
		public void onStart() {
			System.out.println("Start");
		}

		@Override
		public void onFinish() {

		}

		@Override
		public void onFailure(int statusCode, Header[] headers,
				byte[] errorResponse, Throwable e) {
			System.out.println("Fail Register");
			try {
				JSONObject obj = new JSONObject(new String(errorResponse));

				Toast.makeText(ListCriteriaActivity.this,
						obj.getString("message").toString(), Toast.LENGTH_SHORT)
						.show();

			} catch (Exception ex) {
				Toast.makeText(ListCriteriaActivity.this,
						getString(R.string.connection_error),
						Toast.LENGTH_SHORT).show();
			}

			onBackPressed();
		}
	};

}
